<?php
/**
 * Created by PhpStorm.
 * User: taoyufang
 * Date: 17/12/05
 * Time: 16:52
 */

class TaskPrizeModel extends MY_Model{

    protected $table = 't_task_prize';

    public function __construct(){
        parent::__construct();
    }

    /**
     * 获取奖励列表
     * name: getList
     * @param string $like
     * @param int $page
     * @param int $pagesize
     * @return array
     * @Author:taiyang
     */
    public function getPrizeList($like = '',$page = 1, $pagesize = 30,$category=-1){
        $sql="select * from ".$this->table." WHERE 1 ";
        if(!empty($like)){
            $sql .= " and `name` like '%".$like."%' ";
        }
        if($category>-1){
            $sql .= " and `category` = '".$category."' ";
        }else{
            $groupList = $this->config->config['prize_category'];
            $key=array_keys($groupList);
            $key_str=implode(',',$key);
            $sql .= " and `category` in(".$key_str.") ";
        }
        $sql.=" order by category asc,prize_data+0 desc ";
        if($page != 0){ //等于0 不分页
            $index = ($page - 1) * $pagesize > 0 ? ($page - 1) * $pagesize : 0;
            $sql.= " limit ".$index.",".$pagesize;
        }
        $res=$this->query_sql_list($sql);

        $res_x=array();
        foreach($res as $k=>$v){
            $v['name']=$v['name'].$this->config->config['prize_category'][$v['category']]['name'];
            $res_x[$v['id']]=$v;
        }
        return $res_x;
    }

    /**
     * 获取奖品总数量
     * name: getListCnt
     * @param string $like
     * @return int
     * @Author:taiyang
     *
     */
    public function getListCnt($like = '',$category=-1){
        $sql="select count(*) from ".$this->table." WHERE 1 ";
        if(!empty($like)){
            $sql .= " and `name` like '%".$like."%' ";
        }
        if($category>-1){
            $sql .= " and `category` = '".$category."' ";
        }else{
            $groupList = $this->config->config['prize_category'];
            $key=array_keys($groupList);
            $key_str=implode(',',$key);
            $sql .= " and `category` in(".$key_str.") ";
        }
        return $this->query_sql_count($sql);
    }

    public function get_id($id){
        $sql='select * from '.$this->table.' where 1 ';
        if(!empty($id)){
            $sql.=" and id=".$id."";
        }else{
            return array();
        }
        return $this->query_sql_one($sql);
    }


}